package org.oyrm.kobo.formBuilder.ui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.oyrm.kobo.formBuilder.actions.PostProcessorOptionsAction;
import org.oyrm.kobo.formBuilder.actions.StartPostProcessorAction;
import org.oyrm.kobo.formBuilder.controller.IFormBuilderListener;
import org.oyrm.kobo.formBuilder.settings.Settings;

/**
 * This class sets up the main menu bar for use in the form builder.
 * 
 * @author etherton
 *
 */
public class MainMenuBar extends JMenuBar{

	/**
	 * To ensure compatibility with JMenuBar
	 */
	private static final long serialVersionUID = 7832986419809959289L;
	
	/********************************************************************
	 * declare class variables
	 ********************************************************************/
		
	/**
	 * Menus for the menu bar
	 */
	private JMenu mainMenu, questionsMenu, addQuestionsMenu, 
					editMenu, i18nMenu, langaguesMenu, postProcessingMenu = null;
	
	/**
	 * Menu items for the mainMenu
	 */
	private JMenuItem loadFromTextMi, viewFormTextMi, openFileMi, saveFileMi, saveFileAsMi,
					exitMi = null;
	
	/**
	 * Menu Items for questions menu
	 */
	private JMenuItem addSelectOptionMi, deleteQuestionMi = null;
	
	/**
	 * Menu Items for adding questions menu
	 */
	private JMenuItem textMi, integerMi, decimalMi, dateMi, multiSelectMi, singleSelectMi,
			timeMi, dateTimeMi, pictureMi, videoMi, audioMi, gpsMi, barcodeMi, labelMi,
			repeatMi, groupMi = null;
	
	/**
	 * Menu Items for edit menu
	 */
	private JMenuItem cutMi, copyMi, pasteMi = null;
	
	/**
	 * Menu Items for localization menu
	 */
	private JMenuItem editLocalesMi = null;
	
	/**
	 * Menu Items for Post Processor
	 */
	private JMenuItem postProcessorMi, postProcessorSetOptionsMi = null;
	
	private IFormBuilderListener controller = null;
	
	
	/********************************************************************
	 * Methods for DesktopFormBuilderFrame
	 ********************************************************************/
	
	/**
	 * Default constructor for this class
	 */
	public MainMenuBar(IFormBuilderListener controller)
	{
		super();
		this.controller = controller;
		createComponents();
		placeComponents();		
	}


	/**
	 * Creates and initializes all components of this class
	 */
	private void createComponents() {
		//create the menus
		mainMenu = new JMenu(Settings.getI18n().getString("mainMenuText"));
		questionsMenu = new JMenu(Settings.getI18n().getString("questionsMenuText"));
		addQuestionsMenu = new JMenu(Settings.getI18n().getString("addQuestionsMenuText")); 
		editMenu = new JMenu(Settings.getI18n().getString("editMenuText"));
		i18nMenu = new JMenu(Settings.getI18n().getString("i18nMenuText"));
		langaguesMenu = new JMenu(Settings.getI18n().getString("langaguesMenuText"));
		postProcessingMenu = new JMenu(Settings.getI18n().getString("postProcessingMenuText"));
		
		//create the main menu items
		loadFromTextMi = new JMenuItem(Settings.getI18n().getString("loadFromText"));
		viewFormTextMi = new JMenuItem(Settings.getI18n().getString("viewFormText"));
		openFileMi = new JMenuItem(Settings.getI18n().getString("openFileText"));
		openFileMi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		saveFileMi = new JMenuItem(Settings.getI18n().getString("SaveFileText"));
		saveFileMi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		saveFileAsMi = new JMenuItem(Settings.getI18n().getString("SaveFileAsText"));
		exitMi = new JMenuItem(Settings.getI18n().getString("exitMenuItemText"));
		
		//create the Menu Items for questions menu		 
		addSelectOptionMi = new JMenuItem(Settings.getI18n().getString("AddSelectOptionText"));
		deleteQuestionMi = new JMenuItem(Settings.getI18n().getString("DeleteQuestionText"));
		
		//create Menu Items for adding questions menu
		textMi = new JMenuItem(Settings.getI18n().getString("TextQuestionText"));
		integerMi = new JMenuItem(Settings.getI18n().getString("IntegerQuestionText"));
		decimalMi = new JMenuItem(Settings.getI18n().getString("DecimalQuestionText"));
		dateMi = new JMenuItem(Settings.getI18n().getString("DateQuestionText"));
		multiSelectMi = new JMenuItem(Settings.getI18n().getString("MultiSelectQuestionText"));
		singleSelectMi = new JMenuItem(Settings.getI18n().getString("SingleSelectQuestionText"));
		timeMi = new JMenuItem(Settings.getI18n().getString("TimeQuestionText"));
		dateTimeMi = new JMenuItem(Settings.getI18n().getString("DateTimeQuestionText"));
		pictureMi = new JMenuItem(Settings.getI18n().getString("PictureQuestionText"));
		videoMi = new JMenuItem(Settings.getI18n().getString("VideoQuestionText"));
		audioMi = new JMenuItem(Settings.getI18n().getString("AudioQuestionText"));
		gpsMi = new JMenuItem(Settings.getI18n().getString("GpsQuestionText"));
		barcodeMi = new JMenuItem(Settings.getI18n().getString("BarcodeQuestionText"));
		labelMi = new JMenuItem(Settings.getI18n().getString("LabelQuestionText"));
		repeatMi = new JMenuItem(Settings.getI18n().getString("RepeatQuestionText"));
		groupMi = new JMenuItem(Settings.getI18n().getString("GroupQuestionText"));
		
		//menu Items for edit menu		
		cutMi = new JMenuItem(Settings.getI18n().getString("cutText"));
		cutMi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		copyMi = new JMenuItem(Settings.getI18n().getString("copyText"));
		copyMi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
		pasteMi = new JMenuItem(Settings.getI18n().getString("pasteText"));
		pasteMi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
		
		//Menu Items for localization menu
		editLocalesMi = new JMenuItem(Settings.getI18n().getString("editLocalesText"));
		
		//Menu Items for Post Processor
		postProcessorMi = new JMenuItem(new StartPostProcessorAction(controller));
		postProcessorMi.setText(Settings.getI18n().getString("postProcessorMenuItemText"));
		
		postProcessorSetOptionsMi = new JMenuItem(new PostProcessorOptionsAction(controller));
		postProcessorSetOptionsMi.setText(Settings.getI18n().getString("postProcessorOptionsMenuItemText"));
		postProcessorSetOptionsMi.setEnabled(false);
	}//end method


	/**
	 * Puts all the components of this class in the right place.
	 */
	private void placeComponents() {
		mainMenu.add(loadFromTextMi);
		mainMenu.add(viewFormTextMi);
		mainMenu.add(openFileMi);
		mainMenu.add(saveFileMi);
		mainMenu.add(saveFileAsMi);
		mainMenu.add(exitMi);
		
		addQuestionsMenu.add(textMi);
		addQuestionsMenu.add(integerMi);
		addQuestionsMenu.add(decimalMi);
		addQuestionsMenu.add(dateMi);
		addQuestionsMenu.add(multiSelectMi);
		addQuestionsMenu.add(singleSelectMi);
		addQuestionsMenu.add(timeMi);
		addQuestionsMenu.add(dateTimeMi);
		addQuestionsMenu.add(pictureMi);
		addQuestionsMenu.add(videoMi);
		addQuestionsMenu.add(audioMi);
		addQuestionsMenu.add(gpsMi);
		addQuestionsMenu.add(barcodeMi);
		addQuestionsMenu.add(labelMi);
		addQuestionsMenu.add(repeatMi);
		addQuestionsMenu.add(groupMi);
		
		questionsMenu.add(addQuestionsMenu);
		questionsMenu.add(addSelectOptionMi);
		questionsMenu.add(deleteQuestionMi);
		
		editMenu.add(cutMi);
		editMenu.add(copyMi);
		editMenu.add(pasteMi);

		langaguesMenu.add(new JMenuItem("test Langauge")); // TODO Figure out how to handle languages

		i18nMenu.add(editLocalesMi); 
		i18nMenu.add(langaguesMenu);


		
		//Menu Items for Post Processor
		postProcessingMenu.add(postProcessorMi);
		postProcessingMenu.add(postProcessorSetOptionsMi);
		
		this.add(mainMenu);
		this.add(questionsMenu);
		this.add(editMenu);
		this.add(i18nMenu);
		this.add(postProcessingMenu);
		
	}//end method
	
	
	/**
	 * This method is used to turn off or on the post proc options
	 * @param showOptions - true if they should be shown, false otherwise
	 */
	public void showPostProcOptions(boolean showOptions)
	{
		postProcessorSetOptionsMi.setEnabled(showOptions);
	
	}

}
